namespace et {
    export class app extends egret.EventDispatcher {
        /** 游戏版本号 */
        public static version: string = "1.0.0";
        /** 
         * 资源版本号
         * 用于cdn资源管理
         */
        public static resVersion: string = "1.0.0";
        /**
         * 平台
         */
        public platform: Platform;
        /**
         * 游戏舞台
         */
        public stage: egret.Stage;
        /** 全局实例 */
        public static instance: app;
        /**
         * 系统信息
         */
        public systemInfo: api.SystemInfo;
        /** 游戏信息缓存 */
        private appInfoCache: {[name: string]: AppInfo} = {};
        /** 游戏banner广告 */
        private adBanner: api.BannerAd;
        /** 广告是否有错误 */
        private bannerHasError: boolean = false;

        /**
         * 初始化游戏基本设置
         * @param stage 游戏舞台 
         */
        public init(stage: egret.Stage){
            app.instance = this;
            
            this.stage = stage;
            this.checkPlatform();
            this.listenLifeCycle();
        }

        /**
         * 初始化游戏信息
         * @param appInfo 游戏信息
         */
        public initApp(appInfo: AppInfo){
            if (this.appInfoCache[appInfo.platform])
                throw "不允许对同一平台多次设置广告信息" + appInfo.platform;

            this.appInfoCache[appInfo.platform] = appInfo;
        }

        /**
         * 获取游戏信息
         * @param platform 游戏平台
         */
        public getAppInfo(platform?: Platform){
            if (platform){
                return this.appInfoCache[platform];
            }

            return this.appInfoCache[this.platform];
        }

        /**
         * 显示banner广告
         */
        public async showBannerAd(){
            if (!this.adBanner){
                /** 加载banner */
                this.adBanner = api.createBannerAd({
                    adUnitId: this.getAppInfo().bannerId,
                    style: {
                        left: 0,
                        top: this.systemInfo.screenHeight,
                        width: this.systemInfo.screenWidth * 4 / 5,
                        height: 120
                    }
                });

                this.adBanner.onError((res) => {
                    /** banner标识为错误 */
                    this.bannerHasError = true;
                });
        
                this.adBanner.onLoad(() => {
                    /** banner标识为错误 */
                    this.bannerHasError = false;
                });

                this.adBanner.onResize((size) => {
                    let sysInfo = this.systemInfo;
                    this.adBanner.style.left = sysInfo.screenWidth / 2 - size.width / 2;
                    this.adBanner.style.top = sysInfo.screenHeight - 1.2 * size.height;
                });
            }

            if (this.adBanner){
                await this.adBanner.show();
            }
        }

        /**
         * 检查平台
         */
        private async checkPlatform(){
            if (window["wx"]){
                window["api"] = window["wx"];
                this.platform = Platform.Wxgame;
                return;
            }

            if (window["qq"]){
                window["api"] = window["qq"];
                this.platform = Platform.QQ;
                return;
            }

            if (window["tt"]){
                window["api"] = window["tt"];
                this.platform = Platform.TT;
                return;
            }

            if (window["qg"]){
                window["api"] = window["qg"];
                this.platform = Platform.Vivo;
                return;
            }

            this.systemInfo = await api.getSystemInfoSync();
        }

        /** 监听生命周期 */
        private listenLifeCycle(){
            api.onShow(this.EtOnShow);
            api.onHide(this.EtOnHide);
        }

        private EtOnShow(res: api.ShowCallback){
            egret.ticker.resume();
            /** 用于解决文本框黑色的问题 */
            egret.startTick(this.refresh, this);

            this.dispatchEvent(new egret.Event(egret.Event.ACTIVATE, true, true, res));
        }

        private EtOnHide(){
            this.dispatchEvent(new egret.Event(egret.Event.DEACTIVATE));
            egret.stopTick(this.refresh, this);
            egret.callLater(egret.ticker.pause, this);
        }

        private _lastTimer: number = 0;
        private refresh(stepTime: number) {
            if (stepTime - this._lastTimer < 2000) return false;
            this._lastTimer = stepTime;
            egret.sys.systemRenderer["canvasRenderer"] = null;
            egret.sys.systemRenderer["canvasRenderBuffer"] = null;
            egretUtils.cleanText(this.stage);
        }
    }

    /**
     * app广告
     */
    export class AppInfo{
        /** 平台 */
        platform: Platform;
        /** appid */
        appId?: string;
        /** 激励视频广告id */
        videoId?: string;
        /** bannerid */
        bannerId?: string;

        constructor(platform: Platform){
            this.platform = platform;
        }
    }

    export enum Platform{
        Wxgame,
        QQ,
        TT,
        Vivo
    }
}